package com.iman.manjusri.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import com.iman.common.hibernate.HibernateSupport;
import com.iman.common.hibernate.TemplateQLService;
import com.iman.common.support.Page;
import com.iman.manjusri.domain.User;
import com.iman.manjusri.mappers.DrmUserSinfoMapper;
import com.iman.manjusri.mappers.UserMapper;
import com.iman.manjusri.mappers.models.DrmUserSinfo;
import com.iman.manjusri.mappers.models.DrmUserSinfoExample;
import com.iman.manjusri.service.UserService;
import com.iman.manjusri.vo.UserSInfoVo;

@Service("userService")
public class UserServiceImpl implements UserService {
	@Inject
	UserMapper userMapper;
    @Inject
    TemplateQLService templateQLService;

    @Inject
    HibernateSupport  hibernateSupport;

    @Inject
    DrmUserSinfoMapper drmUserSinfoMapper;
    
    
    @Override
    public User getUserById(String userId) {
        Map root = new HashMap();
        root.put("userId", userId);
        try {
            User user = userMapper.getTeacherInfo(root);
            return user;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new User();
    }

    @Override
    public User getUserByUserIdPwd(String userId, String password) {
        @SuppressWarnings("unchecked")
        List<User> userList = (List<User>)hibernateSupport.find(" from User where userId=? and pwd=?", userId, password);
        if (null != userList && userList.size() > 0) {
            return userList.get(0);
        }
        return null;
    }

    public User getUserByName(String userName) {
        @SuppressWarnings("unchecked")
        List<User> userList = (List<User>)hibernateSupport.find(" from User where loginName=?", userName);
        if (null != userList && userList.size() > 0) {
            return userList.get(0);
        }
        return null;
    }

    @Override
    public void updatePwd(String userId, String password) {
        User user = this.hibernateSupport.get(User.class, userId);
        user.setPwd(password);
        this.hibernateSupport.merge(user);
    }

	@Override
	public void updatePass(String loginName, String salt, String pass) {
		Map<String, String> mapInfo = new HashMap<String, String>();
		mapInfo.put("loginName", loginName);
		mapInfo.put("salt", salt);
		mapInfo.put("pass", pass);
		templateQLService.executeSQLUpdate("update_user_pass", mapInfo);
	}

	@Override
	public List<User> getUserList(Map<String, Object> mapInfo) {
		return userMapper.getUserList(mapInfo);		
	}

	@Override
	public User getUserById(Map<String, Object> mapInfo) {		
		return userMapper.getTeacherInfo(mapInfo);
	}

	@Override
	public Integer getUserCnt(Map<String, Object> mapInfo){
		BigInteger cnt = (BigInteger) templateQLService.findSQL("get_user_cnt", mapInfo);
		return cnt.intValue();
	}

	
	/**
	 * 获取用户的 积分等级信息
	 */
	@Override
	public UserSInfoVo getUserSInfoById(Long userId) {
		UserSInfoVo vo = new UserSInfoVo();
		DrmUserSinfo  infos = drmUserSinfoMapper.selectByPrimaryKey(userId);
		if(infos != null ){
			try {
				BeanUtils.copyProperties(vo, infos);
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		
		return vo;
	}

}
